import useAccountStore from "@/store/modules/AccountStore.js";
import request from "@/utils/request.js";
import {desDecrypt, genDesKey, rsaPubEncrypt} from "@/utils/EncryptUtils.js";
import {elMessageTip} from "@/utils/MessageTipUtils.js";
import {resolvePromise_data, resolvePromise_status} from "@/utils/ReturnUtils.js";
import {del, post} from "@/utils/http.js";
import {strIsNull} from "@/utils/StringUtils.js";

/**
 * 新增商品分类
 *
 * @param name
 * @param code
 * @param disabled
 * @param description
 * @param subs
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const addProductCategoryAPI = (name, code, disabled, description, subs) => {
    return request.post(
        '/product/category',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            name: rsaPubEncrypt(name),
            code: rsaPubEncrypt(code),
            disabled,
            description: description === null || description.length <= 0 ? null : rsaPubEncrypt(description),
            subs: subs.map(sub => {
                const subTemp = {...sub}
                subTemp.id = null
                subTemp.name = rsaPubEncrypt(subTemp.name)
                subTemp.code = rsaPubEncrypt(subTemp.code)
                let subTempDesc = subTemp.description
                subTemp.description = subTempDesc === null || subTempDesc.length <= 0 ? null : rsaPubEncrypt(subTemp.description)
                return subTemp
            })
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 新增二级商品分类
 *
 * @param name
 * @param code
 * @param disabled
 * @param description
 * @param productCategoryId
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const addProductCategorySubAPI = (productCategoryId, name, code, disabled, description) => {
    return request.post(
        '/product/category/sub',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            productCategoryId,
            name: rsaPubEncrypt(name),
            code: rsaPubEncrypt(code),
            disabled,
            description: description === null || description.length <= 0 ? null : rsaPubEncrypt(description)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 更新商品分类
 *
 * @param id
 * @param name
 * @param code
 * @param disabled
 * @param description
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const updateProductCategoryAPI = (id, name, code, disabled, description) => {
    return request.put(
        '/product/category',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id,
            name: rsaPubEncrypt(name),
            code: rsaPubEncrypt(code),
            disabled,
            description: description === null || description.length <= 0 ? null : rsaPubEncrypt(description)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 更新商品二级分类
 *
 * @param id
 * @param productCategoryId
 * @param name
 * @param code
 * @param disabled
 * @param description
 * @returns {Promise<axios.AxiosResponse<any>>}
 */
export const updateProductCategorySubAPI = (id, productCategoryId, name, code, disabled, description) => {
    return request.put(
        '/product/category/sub',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id,
            productCategoryId,
            name: rsaPubEncrypt(name),
            code: rsaPubEncrypt(code),
            disabled,
            description: description === null || description.length <= 0 ? null : rsaPubEncrypt(description)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 商品分类删除
 *
 * @param id
 * @param toId
 * @returns {Promise<axios.AxiosResponse<*>>}
 */
export const delProductCategoryAPI = (id, toId) => {
    return del(
        '/product/category',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id,
            toId
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const delProductCategorySubAPI = (id, toId, toSubId) => {
    return del(
        '/product/category/sub',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id, toId, toSubId
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 分页获取商品分类
 *
 * @param current
 * @param pageSize
 * @param disabled
 * @param nameMatch
 * @param codeMatch
 * @returns {Promise<*>}
 */
export const getProductCategoryPageAPI = (current, pageSize, disabled = null, nameMatch = null, codeMatch = null) => {
    const desKey = genDesKey()
    return request.get(
        '/product/category/page',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey),
                current, pageSize,
                disabled: disabled === null || disabled === '' ? null : disabled,
                nameMatch: nameMatch === null || nameMatch === '' ? null : rsaPubEncrypt(nameMatch),
                codeMatch: codeMatch === null || codeMatch === '' ? null : rsaPubEncrypt(codeMatch)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

/**
 * 获取全部的商品分类
 *
 * @returns {Promise<*>}
 */
export const getProductCategoryAllAPI = () => {
    const desKey = genDesKey()
    return request.get(
        '/product/category/all',
        {
            params: {
                account: rsaPubEncrypt(useAccountStore().account),
                desKey: rsaPubEncrypt(desKey)
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

/**
 * 上传商品数据
 *
 * @param title
 * @param productCategoryId
 * @param productCategorySubId
 * @param freight
 * @param delivery
 * @param address
 * @param imgList
 * @param options
 * @param labelNames
 * @param params
 * @param description_md
 * @param description_html
 * @param id
 */
export const uploadProductAPI = (
    title, productCategoryId, productCategorySubId, freight, delivery, address,
    imgList, options, labelNames, params, description_md, description_html, code, id = null
) => {
    return request.post(
        '/product/upload',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id: id === null ? null : id,
            code: rsaPubEncrypt(code),
            title: rsaPubEncrypt(title),
            productCategoryId,
            productCategorySubId,
            freight: freight,
            delivery: rsaPubEncrypt(delivery),
            address: rsaPubEncrypt(address),
            descriptionMd: rsaPubEncrypt(description_md),
            descriptionHtml: rsaPubEncrypt(description_html),
            imgList: imgList.map(item => rsaPubEncrypt(item)),
            labelNames: labelNames.map(item => rsaPubEncrypt(item)),
            options: options.map(item => {
                const i = {...item}
                i.name = rsaPubEncrypt(i.name)
                i.img = rsaPubEncrypt(i.img)
                return i
            }),
            params: params.map(item => {
                const i = {...item}
                i.name = rsaPubEncrypt(i.name)
                i.val = rsaPubEncrypt(i.val)
                return i
            }),
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const getProductPageAPI = (
    /**
     * 分页获取商品
     *
     * @param current
     * @param pageSize
     * @param title
     * @param category
     * @param categorySub
     * @param delivery
     * @param freight
     * @param label
     * @param param
     * @param isRemove
     * @param code
     * @returns {Promise<*>}
     */
    current, pageSize,
    title = null, category = null, categorySub = null, delivery = null, freight = null,
    label = null, param = null, isRemove = null, code = null
) => {
    const desKey = genDesKey()
    return request.get(
        '/product/page',
        {
            params: {
                desKey: rsaPubEncrypt(desKey),
                current, pageSize,
                title: title === null || title === '' ? null : rsaPubEncrypt(title),
                code: code === null || code === '' ? null : rsaPubEncrypt(code),
                category: category === null || category === '' ? null : category,
                categorySub: categorySub === null || categorySub === '' ? null : categorySub,
                delivery: delivery === null || delivery === '' ? null : rsaPubEncrypt(delivery),
                freight: freight === null || freight === '' ? null : freight,
                isRemove: isRemove === null || isRemove === '' ? null : isRemove,
                label: label === null || label === '' ? null : rsaPubEncrypt(label),
                param: param === null || param === '' ? null : rsaPubEncrypt(param),
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            console.log(res)
            elMessageTip(res)
            return resolvePromise_data(res)
        }
    )
}

/**
 * 逻辑删除商品
 *
 * @param id
 * @returns {Promise<axios.AxiosResponse<*>>}
 */
export const delProductLogicAPI = (id) => {
    return del(
        `/product/${id}/logic`,
        {
            account: rsaPubEncrypt(useAccountStore().account)
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

/**
 * 上传商品数据
 *
 * @param title
 * @param productCategoryId
 * @param productCategorySubId
 * @param freight
 * @param delivery
 * @param address
 * @param imgList
 * @param options
 * @param labelNames
 * @param params
 * @param description_md
 * @param description_html
 * @param isRemove
 * @param id
 */
export const updateProductAPI = (
    title, productCategoryId, productCategorySubId, freight, delivery, address,
    imgList, options, labelNames, params, description_md, description_html, isRemove, code, id = null
) => {
    return request.post(
        '/product/update',
        {
            account: rsaPubEncrypt(useAccountStore().account),
            id: id === null ? null : id,
            title: rsaPubEncrypt(title),
            code: rsaPubEncrypt(code),
            productCategoryId,
            productCategorySubId,
            // isRemove,
            freight: freight,
            delivery: rsaPubEncrypt(delivery),
            address: rsaPubEncrypt(address),
            descriptionMd: rsaPubEncrypt(description_md),
            descriptionHtml: rsaPubEncrypt(description_html),
            imgList: imgList.map(item => rsaPubEncrypt(item)),
            labelNames: labelNames.map(item => rsaPubEncrypt(item)),
            options: options.map(item => {
                const i = {...item}
                i.name = rsaPubEncrypt(i.name)
                i.img = rsaPubEncrypt(i.img)
                return i
            }),
            params: params.map(item => {
                const i = {...item}
                i.name = rsaPubEncrypt(i.name)
                i.val = rsaPubEncrypt(i.val)
                return i
            }),
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}

export const getCommentById = (productId, userId = null) => {
    const desKey = genDesKey()
    return request.get(
        `/product/comment`,
        {
            params: {
                desKey: rsaPubEncrypt(desKey),
                productId, userId
            }
        }
    ).then(
        res => {
            res = desDecrypt(res, desKey)
            return resolvePromise_data(res)
        }
    )
}

export const delCommentById = (commentId) => {
    return post(
        `/product/comment/del`,
        {
            account: rsaPubEncrypt(useAccountStore().account),
            commentId
        }
    ).then(
        res => {
            elMessageTip(res)
            return resolvePromise_status(res)
        }
    )
}